package com.aos.controller;

import com.aos.entity.Application;
import com.aos.permission.RequiredPermission;
import com.aos.service.application.ApplicationService;
import com.aos.util.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ApplicationController
 * @Description:应用控制器
 * @Author wumingwang
 * @Date 2018/9/5 17:19
 * @Version 1.0
 */
@Slf4j
@RestController
@Api(value = "Application Controller", tags = {"应用接口"})
public class ApplicationController {

    @Autowired
    private ApplicationService applicationService;


    /**
     * 分页查询应用
     * @param name
     * @param contact
     * @param startDate
     * @param endDate
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询应用", notes = "分页查询应用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "应用名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "contact", value = "联系人",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "应用创建起始时间",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "应用创建结束时间",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "int" ,example = "1"),
            @ApiImplicitParam(name = "limit", value = "页面大小", required = true, paramType = "query", dataType = "int",example = "10"),
    })
    @RequiredPermission
    @GetMapping("/application/v1/queryByPage")
    public Object queryByPage(String name,String contact,String startDate,String endDate,int page, int limit){
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("contact",contact);
        map.put("startDate",startDate);
        if(!StringUtils.isEmpty(endDate)){
            endDate = endDate + " 23:59:59";
        }
        map.put("endDate",endDate);
        map.put("pageNum",page);
        map.put("pageSize",limit);
        return applicationService.queryByPage(map);
    }

    /**
     * 新增应用
     * @param application
     * @return
     */
    @ApiOperation(value = "新增应用", notes = "新增应用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "应用代码",  required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "应用名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "系统状态【1:可用 默认；2不可用】", required = true, dataType = "String"),
            @ApiImplicitParam(name = "contact", value = "联系人", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "联系电话", dataType = "String"),
            @ApiImplicitParam(name = "permissionUpdateUrl", value = "应用权限实时更新接口url地址", dataType = "String"),
    })
    @RequiredPermission
    @PostMapping("/application/v1/add")
    public Object addApplication(@Validated @RequestBody Application application){
        return applicationService.addApplication(application);
    }

    /**
     * 删除应用
     * @param id
     * @return
     */
    @ApiOperation(value = "删除应用", notes = "删除应用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "应用ID",  required = true, dataType = "Long",example = "0"),
    })
    @RequiredPermission
    @DeleteMapping("/application/v1/delete")
    public Object deleteApplication(Long id){
        if(id == null){
            return Result.error("应用ID不能为空！");
        }
        return applicationService.updateStateForDelete(id);
    }

    /**
     * 编辑应用
     * @param application
     * @return
     */
    @ApiOperation(value = "编辑应用", notes = "编辑应用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "应用ID",  required = true, dataType = "Long", example = "0"),
            @ApiImplicitParam(name = "code", value = "应用代码",  required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "应用名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "系统状态【1:可用 默认；2不可用】", required = true, dataType = "String"),
            @ApiImplicitParam(name = "contact", value = "联系人", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "联系电话", dataType = "String"),
            @ApiImplicitParam(name = "permissionUpdateUrl", value = "应用权限实时更新接口url地址", dataType = "String"),
    })
    @RequiredPermission
    @PutMapping("/application/v1/update")
    public Object update(@Validated @RequestBody Application application){
        if(application.getId() == null){
            return Result.error("应用ID不能为空！");
        }
        return applicationService.update(application);
    }

    /**
     * 查询应用详情
     * @param id
     * @return
     */
    @ApiOperation(value = "查询应用详情", notes = "查询应用详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "应用ID",  required = true, dataType = "Long",example = "0"),
    })
    @RequiredPermission
    @GetMapping("/application/v1/queryDetails")
    public Object queryDetails(Long id){
        if(id == null){
            return Result.error("应用ID不能为空！");
        }
        return applicationService.queryDetails(id);
    }

    /**
     * 查询所有应用列表
     * @return
     */
    @ApiOperation(value = "查询所有应用", notes = "查询所有应用列表")
    @GetMapping("/application/v1/queryAll")
    public Object queryAll(){
        return applicationService.queryAll();
    }
}
